package com.njh.kaleidoscope.common.channel;

import android.content.Context;
import android.content.pm.ApplicationInfo;

import com.jh.tools.base.BaseApp;
import com.jh.tools.common.phone.PhoneUtils;
import com.jh.tools.common.string.StringUtils;
import com.jh.tools.constant.Constants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ChannelData {
    // 套壳自动设置
    public static String channelId = null;

    /**
     * 渠道号
     */
    public static final String CHA_PRO_COM = "tecool_common_";
    public static final String CHA_PRO_COM_TAG = "tecool_com";
    public static final String CHA_PRO_CLEAN_TAG = "tecool_cle";
    public static final String CHA_PRO_CLE = "tecool_clean_";
    public static final String CHA_DEF = "show_01_channel_01_100000";

    public static String appChannelId = null;

    public static String getAppChannelId(Context context) {
        if (channelId != null) {
            return channelId;
        }
        if (null == appChannelId) {
            appChannelId = readZipFile(context, "sychannel");
        }
        if (appChannelId == null || "".equals(appChannelId)
                || "null".equals(appChannelId) || "NULL".equals(appChannelId)) {
            String chMain = PhoneUtils.getMetaData(BaseApp.getmContext(),
                    Constants.SysConstant.CHANNEL_ID);
            if (!StringUtils.isEmpty(chMain)) {
                appChannelId = chMain;
            } else {
                appChannelId = CHA_DEF;
            }
        }
        return appChannelId;
    }

    public static String readZipFile(Context context, String channelKey) {
        String line = null;
        // 从apk包中获取
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        // 注意这里：默认放在meta-inf/里， 所以需要再拼接一下
        String key = "META-INF/" + channelKey;
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.startsWith(key)) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(zipfile.getInputStream(entry)));
                    while ((line = br.readLine()) != null) {
                        return line;
                    }
                    br.close();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return line;
    }

}
